package com.jxl.coreplay.managers
{
	import com.jxl.coreplay.constants.VideoConstants;
	import com.jxl.coreplay.net.CorePlayNetConnection;
	import com.jxl.coreplay.vo.StreamConfigurationVO;
	

	public class ProgressiveStreamManager extends AbstractStreamManager
	{
		
		public function ProgressiveStreamManager(videoType:StreamConfigurationVO):void
		{
			super(videoType);
			
			if(videoType.type != VideoConstants.PROGRESSIVE)
			{
				throw new Error("ProgressiveStreamManager can only handle progressive video types.");
				return;
			}
			
			initProgressive();
		}
		
		protected function initProgressive():void
		{
			if(corePlayNetConnection == null)
			{
				corePlayNetConnection = new CorePlayNetConnection(VideoConstants.CONNECTION_PROGRESSIVE);
			}
			
			createNetStream(corePlayNetConnection.netConnection);
			_videoType.video.attachNetStream(corePlayNetStream);
			if(_videoType.autoPlay == true)
			{
				play();
			}
		}
		
		protected function playProgressive():void
		{
			if(corePlayNetStream == null)
			{
				initProgressive();
			}
			corePlayNetStream.play(_videoType.progressiveVideoURL);
		}
		
		protected function stopProgressive():void
		{
			if(corePlayNetStream != null)
			{
				corePlayNetStream.close();
			}
			
			// unlike Pause, if user hits stop, we want to
			// clear the Video object so a lasting image of
			// the video doesn't linger
			try
			{
				_videoType.video.video.clear();
			}
			catch(err:Error)
			{
				// if it's null, that's ok
			}
		}
		
		protected function pauseProgressive():void
		{
			if(corePlayNetStream != null)
			{
				corePlayNetStream.pause();
			}
		}
		
		protected function seekProgressive(offset:Number):void
		{
			if(corePlayNetStream != null)
			{
				corePlayNetStream.seek(offset);
			}
		}
		
		public override function play():void
		{
			playProgressive();
		}
		
		public override function pause():void
		{
			pauseProgressive();
		}
		
		public override function stop():void
		{
			stopProgressive();
		}
		
		public override function seek(offset:Number):void
		{
			seekProgressive(offset);
		}
		
	}
}